package promise;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class SuperPromise {

    private Map<String, ChannelPromise> channelPromiseMap = new HashMap<>();

    public void createPromise(String key, ChannelPromise channelPromise) {
        channelPromiseMap.put(key, channelPromise);
        System.out.println(channelPromise);
    }

    public void setSuccess(String key) {
        channelPromiseMap.get(key).setSuccess();
    }

    public void await(String key) {
        try {
            this.channelPromiseMap.get(key).await(5, TimeUnit.SECONDS);
            this.channelPromiseMap.remove(key);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
